package gui;

import java.awt.EventQueue;
import modelLayer.Product;
import modelLayer.ProductLine;
import modelLayer.Sale;

import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.xml.bind.JAXBException;

import org.xml.sax.SAXException;

import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;

import controlLayer.ProductCtr;
import controlLayer.SaleCtr;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainGui extends JFrame {

	/**
	 * @author Group 8 DM81-83
	 */
	private JPanel contentPane;
	private JTextField carPartNameField_Product;
	private JTextField carPartPriceField_Product;
	private JTextField carPartQuantityField_Product;
	private JTextField carModelField_Product;
	private JTextField carEngineTypeField_Product;
	private JTextField carModel_Search;
	private JTextField carEngineType_Search;
	private JTextField carPartName_Search;
	private JTextField modelField_Sale;
	private JTextField engineTypeField_Sale;
	private JTextField partNameField_Sale;
	private JTextField priceField_Sale;
	private JTextField numberField_Sale;
	private JTextField modelField_Order;
	private JTextField engineTypeField_Order;
	private JTextField partNameField_Order;
	private JTextField priceField_Order;
	private JTextField numberField_Order;
	private static JTable addProductTable;
	private JTable makeSaleTable;
	private JTable table_2;
	private JTextField txtPhoneNumber;
	private static JTable searchProductTable;
	private static ProductCtr productCtr = new ProductCtr();
	private JTextField textField;
	private JComboBox carBrandField_Sale;
	private JLabel availableLabel_Sale;
	private JButton btnAdd_Sale;
	private Sale sale = new Sale();
	private Product currentSaleProduct;
	private ProductLine currentSaleProductLine;
	private JLabel subtotalLabel_Sale;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainGui frame = new MainGui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainGui() {

		setExtendedState(Frame.MAXIMIZED_BOTH);
		setTitle("IF Pongratiu Erika - Stock Management");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenu mnNew = new JMenu("New");
		mnNew.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/plus.png")));
		mnFile.add(mnNew);

		JMenuItem mntmProduct = new JMenuItem("Product");
		mntmProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new NewProductGui().setVisible(true);
			}
		});
		mntmProduct.setSelectedIcon(null);
		mntmProduct
				.setIcon(new ImageIcon(
						MainGui.class
								.getResource("/javax/swing/plaf/metal/icons/ocean/maximize.gif")));
		mnNew.add(mntmProduct);

		JMenuItem mntmReport = new JMenuItem("Report");
		mntmReport
				.setIcon(new ImageIcon(
						MainGui.class
								.getResource("/javax/swing/plaf/metal/icons/ocean/maximize.gif")));
		mnNew.add(mntmReport);

		JSeparator separator_5 = new JSeparator();
		mnNew.add(separator_5);

		JMenuItem mntmClient = new JMenuItem("Client");
		mntmClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new NewClientGui().setVisible(true);
			}
		});
		mntmClient
				.setIcon(new ImageIcon(
						MainGui.class
								.getResource("/javax/swing/plaf/metal/icons/ocean/maximize.gif")));
		mnNew.add(mntmClient);

		JSeparator separator_6 = new JSeparator();
		mnNew.add(separator_6);

		JMenuItem mntmOrder = new JMenuItem("Order");
		mntmOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new NewOrderGui().setVisible(true);
			}
		});
		mntmOrder
				.setIcon(new ImageIcon(
						MainGui.class
								.getResource("/javax/swing/plaf/metal/icons/ocean/maximize.gif")));
		mnNew.add(mntmOrder);

		JSeparator separator_4 = new JSeparator();
		mnFile.add(separator_4);

		JMenuItem mntmCreateAccount = new JMenuItem("Create Account");
		mntmCreateAccount.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new CreateAccountGui().setVisible(true);
			}
		});
		mntmCreateAccount.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/personl.png")));
		mnFile.add(mntmCreateAccount);

		JMenuItem mntmLogIn = new JMenuItem("Log In");
		mntmLogIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new LogInGui().setVisible(true);
			}
		});
		mntmLogIn.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/login.png")));
		mnFile.add(mntmLogIn);

		JMenuItem mntmLogOut = new JMenuItem("Log Out");
		mntmLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		mntmLogOut.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/logout.png")));
		mnFile.add(mntmLogOut);

		JSeparator separator_7 = new JSeparator();
		mnFile.add(separator_7);

		JMenuItem mntmPrint = new JMenuItem("Print");
		mntmPrint.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/print.png")));
		mnFile.add(mntmPrint);

		JSeparator separator_3 = new JSeparator();
		mnFile.add(separator_3);

		JMenuItem mntmSettings = new JMenuItem("Settings");
		mntmSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new SettingsGui().setVisible(true);
			}
		});
		mntmSettings.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/settings.png")));
		mnFile.add(mntmSettings);

		JSeparator separator_10 = new JSeparator();
		mnFile.add(separator_10);

		JMenuItem mntmQuit = new JMenuItem("Quit");
		mntmQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(null, "Are you sure?",
						"Confirm", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					dispose();
				}
			}
		});
		mntmQuit.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/cancel.png")));
		mnFile.add(mntmQuit);

		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);

		JMenuItem mntmCompanyData = new JMenuItem("Company Data");
		mntmCompanyData.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/folder.png")));
		mntmCompanyData.setEnabled(false);
		mnEdit.add(mntmCompanyData);

		JSeparator separator_11 = new JSeparator();
		mnEdit.add(separator_11);

		JMenu mnAccount = new JMenu("Account");
		mnAccount.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/personl.png")));
		mnEdit.add(mnAccount);

		JMenuItem mntmClient_1 = new JMenuItem("Client");
		mnAccount.add(mntmClient_1);

		JMenuItem mntmUser = new JMenuItem("User");
		mnAccount.add(mntmUser);

		JMenu mnProduct = new JMenu("Product");
		mnEdit.add(mnProduct);

		JMenuItem mntmAllFields = new JMenuItem("All fields");
		mnProduct.add(mntmAllFields);

		JSeparator separator_8 = new JSeparator();
		mnProduct.add(separator_8);

		JMenuItem mntmName = new JMenuItem("Name");
		mnProduct.add(mntmName);

		JMenuItem mntmBrand = new JMenuItem("Brand");
		mnProduct.add(mntmBrand);

		JMenuItem mntmModel = new JMenuItem("Model");
		mnProduct.add(mntmModel);

		JMenuItem mntmEngineType = new JMenuItem("Engine type");
		mnProduct.add(mntmEngineType);

		JMenuItem mntmPrice = new JMenuItem("Price");
		mnProduct.add(mntmPrice);

		JMenuItem mntmNumber = new JMenuItem("Stock number");
		mnProduct.add(mntmNumber);

		JSeparator separator_9 = new JSeparator();
		mnEdit.add(separator_9);

		JMenuItem mntmOrder_1 = new JMenuItem("Order");
		mnEdit.add(mntmOrder_1);

		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		JMenuItem mntmCompany = new JMenuItem("Company");
		mnHelp.add(mntmCompany);

		JMenuItem mntmHelpContents = new JMenuItem("User Manual");
		mntmHelpContents.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/star.png")));
		mnHelp.add(mntmHelpContents);

		JSeparator separator_12 = new JSeparator();
		mnHelp.add(separator_12);

		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AboutGui().setVisible(true);
			}
		});
		mntmAbout.setIcon(new ImageIcon(MainGui.class
				.getResource("/Icons/help.png")));
		mnHelp.add(mntmAbout);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

		final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setToolTipText("");
		tabbedPane.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_M) {
					// When keyboard button M is pressed
					tabbedPane.setSelectedIndex(0);// Main Menu tab
				}
				if (e.getKeyCode() == KeyEvent.VK_A) {
					// When keyboard button A is pressed
					tabbedPane.setSelectedIndex(1);// Add product tab
				}
				if (e.getKeyCode() == KeyEvent.VK_R) {
					// When keyboard button R is pressed
					tabbedPane.setSelectedIndex(4);// Reports tab
				}
				if (e.getKeyCode() == KeyEvent.VK_S) {
					// When keyboard button S is pressed
					tabbedPane.setSelectedIndex(2);// Make Sale Tab
				}
				if (e.getKeyCode() == KeyEvent.VK_O) {
					// When keyboard button O is pressed
					tabbedPane.setSelectedIndex(3);// Make Order tab
				}
				if (e.getKeyCode() == KeyEvent.VK_H) {
					// When keyboard button H is pressed
					tabbedPane.setSelectedIndex(4);// Search tab
				}

			}
		});
		contentPane.add(tabbedPane);

		JPanel mainMenuPane = new JPanel();
		tabbedPane.addTab("Main Menu", null, mainMenuPane, null);
		mainMenuPane.setLayout(new MigLayout("", "[20%][20%][20%][20%][20%]",
				"[20%][100px][100px]"));

		JButton btnMakeSale = new JButton(
				"<html>Make <a style='text-decoration:underline'>S</a>ale</html>");
		btnMakeSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tabbedPane.setSelectedIndex(2);// Make Sale Tab
			}
		});
		btnMakeSale.setFocusable(false);
		btnMakeSale.setFont(btnMakeSale.getFont().deriveFont(
				btnMakeSale.getFont().getSize() + 13f));
		mainMenuPane.add(btnMakeSale, "flowx,cell 2 1,grow");

		JButton btnRaports = new JButton(
				"<html><a style='text-decoration:underline'>R</a>eports</html>");
		btnRaports.setFocusable(false);
		btnRaports.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.setSelectedIndex(5);// Reports Tab
			}
		});

		JButton btnMakeOrder = new JButton(
				"<html>Make <a style='text-decoration:underline'>O</a>rder</html>");
		btnMakeOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.setSelectedIndex(3);// Make Order Tab
			}
		});
		btnMakeOrder.setFocusable(false);
		btnMakeOrder.setFont(btnMakeOrder.getFont().deriveFont(
				btnMakeOrder.getFont().getSize() + 13f));
		mainMenuPane.add(btnMakeOrder, "cell 3 1,grow");
		btnRaports.setFont(btnRaports.getFont().deriveFont(
				btnRaports.getFont().getSize() + 13f));
		mainMenuPane.add(btnRaports, "cell 1 2,grow");

		JButton btnAddNewProduct = new JButton(
				"<html><a style='text-decoration:underline'>A</a>dd product</html>");
		btnAddNewProduct.setFocusable(false);

		btnAddNewProduct.setFont(btnAddNewProduct.getFont().deriveFont(
				btnAddNewProduct.getFont().getSize() + 13f));
		btnAddNewProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tabbedPane.setSelectedIndex(1); // Add Product Tab
			}
		});
		mainMenuPane.add(btnAddNewProduct, "cell 1 1,grow");

		JButton btnSearch = new JButton(
				"<html>Searc<a style='text-decoration:underline'>h</a></html>");
		btnSearch.setFocusable(false);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.setSelectedIndex(4);// Search tab
			}
		});
		btnSearch.setFont(btnSearch.getFont().deriveFont(
				btnSearch.getFont().getSize() + 13f));
		mainMenuPane.add(btnSearch, "cell 3 2,grow");

		final JPanel addingPane = new JPanel();
		tabbedPane.addTab("Add Product", null, addingPane, null);
		addingPane.setLayout(new MigLayout("",
				"[grow][grow][grow][grow][grow][grow][grow]",
				"[][][][][][][][][][][grow]"));

		JButton btnMainMenu = new JButton("Main Menu");
		btnMainMenu.setFocusable(false);
		btnMainMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tabbedPane.setSelectedIndex(0);// Main Menu Tab
			}
		});
		btnMainMenu.setFont(new Font("Tahoma", Font.PLAIN, 15));
		addingPane.add(btnMainMenu, "cell 0 0 7 1,growx");

		JSeparator separator_22 = new JSeparator();
		addingPane.add(separator_22, "cell 0 1 7 1,grow");

		JLabel lblNewLabel_1 = new JLabel("Adding products to stock");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 24));
		addingPane.add(lblNewLabel_1, "cell 0 2 8 1,alignx center");

		JSeparator separator = new JSeparator();
		addingPane.add(separator, "cell 0 3 8 1,growx");

		Label label = new Label("Car Brand");
		label.setFont(UIManager.getFont("Button.font"));
		addingPane.add(label, "cell 0 4,alignx center");

		Label label_1 = new Label("Model");
		label_1.setFont(UIManager.getFont("Button.font"));
		addingPane.add(label_1, "cell 1 4,alignx center");

		Label label_2 = new Label("Engine type");
		label_2.setFont(UIManager.getFont("Button.font"));
		addingPane.add(label_2, "cell 2 4,alignx center");

		JLabel lblPartName = new JLabel("Part name");
		lblPartName.setFont(UIManager.getFont("Button.font"));
		addingPane.add(lblPartName, "cell 3 4,alignx center");

		JLabel lblPrice = new JLabel("Price");
		addingPane.add(lblPrice, "cell 4 4,alignx center");

		JLabel lblNewLabel = new JLabel("Quantity");
		addingPane.add(lblNewLabel, "cell 5 4,alignx center");

		final JComboBox<String> carBrandField_Product = new JComboBox<String>();
		carBrandField_Product
				.setToolTipText("Select the car brand or let empty for any");
		carBrandField_Product.setName("carBrand");
		carBrandField_Product.setEditable(true);
		carBrandField_Product.setModel(new DefaultComboBoxModel(new String[] {
				"", "Alfa Romeo", "Audi", "BMW", "Chevrolet", "Chrysler",
				"Citro\u00EBn", "Dacia", "Daewoo", "Daihatsu", "Dodge", "Fiat",
				"Ford", "Honda", "Hyundai", "Iveco", "Jaguar", "Jeep", "Kia",
				"Lancia", "Land Rover", "Lexus", "Mazda", "Mercedes", "Mini",
				"Mitsubishi", "Nissan", "Opel", "Peugeot", "Porsche",
				"Renault", "Rover", "Saab", "Seat", "Skoda", "Smart", "Subaru",
				"Suzuki", "Toyota", "Volkswagen", "Volvo" }));
		addingPane.add(carBrandField_Product, "cell 0 5,grow");

		carModelField_Product = new JTextField();
		addingPane.add(carModelField_Product, "cell 1 5,grow");
		carModelField_Product.setColumns(15);

		carEngineTypeField_Product = new JTextField();
		addingPane.add(carEngineTypeField_Product, "cell 2 5,grow");
		carEngineTypeField_Product.setColumns(15);

		carPartNameField_Product = new JTextField();
		addingPane.add(carPartNameField_Product, "cell 3 5,grow");
		carPartNameField_Product.setColumns(30);

		carPartPriceField_Product = new JTextField();
		addingPane.add(carPartPriceField_Product, "cell 4 5,grow");
		carPartPriceField_Product.setColumns(15);

		carPartQuantityField_Product = new JTextField();
		addingPane.add(carPartQuantityField_Product, "cell 5 5,grow");
		carPartQuantityField_Product.setColumns(8);

		JButton btnAdd = new JButton("Add Product");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				String carBrand = (String) carBrandField_Product
						.getSelectedItem();
				String carModel = carModelField_Product.getText();
				String engineType = carEngineTypeField_Product.getText();
				String partName = carPartNameField_Product.getText();
				if (!partName.equals("")
						&& !carPartPriceField_Product.getText().equals("")
						&& !carPartQuantityField_Product.getText().equals("")) {
					double price = Double.parseDouble(carPartPriceField_Product
							.getText());
					int stockQuantity = Integer
							.parseInt(carPartQuantityField_Product.getText());
					Product product = new Product(-1, carBrand, carModel,
							engineType, partName, price, stockQuantity);

					try {
						Product updateProduct = productCtr
								.searchProduct(product);
						System.out.println("product id="
								+ updateProduct.getProductId());
						if (updateProduct.getProductId() < 0) {
							productCtr.insertProduct(product);
						} else {
							productCtr.addProductsToStock(product,
									updateProduct);
						}
						refreshTables();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(null,
							"Part Name, Price and Quantity can't be emplty.",
							"Input Error!", JOptionPane.YES_OPTION);
				}
			}
		});
		addingPane.add(btnAdd, "cell 6 5,growx");

		JSeparator separator_1 = new JSeparator();
		addingPane.add(separator_1, "cell 0 6 8 1,growx");

		JLabel lblNewLabel_2 = new JLabel("Recent activity");
		lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 24));
		addingPane.add(lblNewLabel_2, "cell 0 7 8 1,alignx center");

		JSeparator separator_2 = new JSeparator();
		addingPane.add(separator_2, "cell 0 8 8 1,growx");

		JScrollPane scrollPane = new JScrollPane();
		addingPane.add(scrollPane, "cell 0 10 8 1,grow");

		addProductTable = new JTable();
		addProductTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int row = addProductTable.getSelectedRow();
				if (row >= 0) {
					String carBrand = (String) addProductTable.getModel()
							.getValueAt(row, 0);
					String carModel = (String) addProductTable.getModel()
							.getValueAt(row, 1);
					String engineType = (String) addProductTable.getModel()
							.getValueAt(row, 2);
					String partName = (String) addProductTable.getModel()
							.getValueAt(row, 3);
					double price = -1;
					int stockQuantity = -1;
					if (addProductTable.getModel().getValueAt(row, 4) != null) {
						price = (double) addProductTable.getModel().getValueAt(
								row, 4);
						stockQuantity = (int) addProductTable.getModel()
								.getValueAt(row, 5);
					}
					Product product = new Product(-1, carBrand, carModel,
							engineType, partName, price, stockQuantity);
					product = productCtr.searchProduct(product);
					if (product.getProductId() >= 0) {
						EditProductGui editProductGui = new EditProductGui(
								product);
						editProductGui.setVisible(true);
					}
				}
			}
		});
		scrollPane.setViewportView(addProductTable);
		addProductTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		addProductTable.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null }, }, new String[] {
				"Car Brand", "Model", "Engine Type", "Name", "Price",
				"Quantity" }) {
			Class[] columnTypes = new Class[] { String.class, String.class,
					String.class, String.class, Double.class, Integer.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			boolean[] columnEditables = new boolean[] { false, false, false,
					false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});

		JPanel makeSalePane = new JPanel();
		tabbedPane.addTab("Make Sale", null, makeSalePane, null);
		makeSalePane.setLayout(new MigLayout("",
				"[grow][grow][grow][grow][grow][grow][grow]",
				"[][][][][][][][][][][grow]"));

		JButton btnMainMenu_1 = new JButton("Main Menu");
		btnMainMenu_1.setFocusable(false);
		btnMainMenu_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.setSelectedIndex(0);// select and show the pane from
												// JTabbedPane with index 0
												// (Main Menu tab)
			}
		});
		btnMainMenu_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
		makeSalePane.add(btnMainMenu_1, "cell 0 0 7 1,growx");

		JSeparator separator_21 = new JSeparator();
		makeSalePane.add(separator_21, "cell 0 1 7 1,grow");

		JLabel lblSelingProducts = new JLabel("Seling Products");
		lblSelingProducts.setFont(new Font("Tahoma", Font.BOLD, 24));
		makeSalePane.add(lblSelingProducts, "cell 0 2 7 1,alignx center");

		JSeparator separator_15 = new JSeparator();
		makeSalePane.add(separator_15, "cell 0 3 7 1,growx");

		JLabel lblCarBrand_2 = new JLabel("Car Brand");
		makeSalePane.add(lblCarBrand_2, "cell 0 4,alignx center");

		JLabel lblModel_3 = new JLabel("Model");
		makeSalePane.add(lblModel_3, "cell 1 4,alignx center");

		JLabel lblEngineType_3 = new JLabel("Engine type");
		makeSalePane.add(lblEngineType_3, "cell 2 4,alignx center");

		JLabel lblPartName_1 = new JLabel("Part name");
		makeSalePane.add(lblPartName_1, "cell 3 4,alignx center");

		JLabel lblPrice_3 = new JLabel("Price");
		makeSalePane.add(lblPrice_3, "cell 4 4,alignx center");

		JLabel lblNumber_2 = new JLabel("Quantity");
		makeSalePane.add(lblNumber_2, "cell 5 4,alignx center");

		JButton btnCancelRefresh = new JButton("Cancel / Refresh product");
		btnCancelRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				availableLabel_Sale.setText("X Available");
				btnAdd_Sale.setEnabled(false);
				carBrandField_Sale.setEnabled(true);
				carBrandField_Sale.setSelectedIndex(0);
				modelField_Sale.setEnabled(true);
				modelField_Sale.setText("");
				engineTypeField_Sale.setEnabled(true);
				engineTypeField_Sale.setText("");
				partNameField_Sale.setEnabled(true);
				partNameField_Sale.setText("");
				numberField_Sale.setEnabled(true);
				numberField_Sale.setText("");
				priceField_Sale.setEnabled(true);
				priceField_Sale.setText("");
			}
		});
		makeSalePane.add(btnCancelRefresh, "cell 6 4,growx");

		carBrandField_Sale = new JComboBox();
		carBrandField_Sale.setModel(new DefaultComboBoxModel(new String[] { "",
				"Alfa Romeo", "Audi", "BMW", "Chevrolet", "Chrysler",
				"Citro\u00EBn", "Dacia", "Daewoo", "Daihatsu", "Dodge", "Fiat",
				"Ford", "Honda", "Hyundai", "Iveco", "Jaguar", "Jeep", "Kia",
				"Lancia", "Land Rover", "Lexus", "Mazda", "Mercedes", "Mini",
				"Mitsubishi", "Nissan", "Opel", "Peugeot", "Porsche",
				"Renault", "Rover", "Saab", "Seat", "Skoda", "Smart", "Subaru",
				"Suzuki", "Toyota", "Volkswagen", "Volvo" }));
		makeSalePane.add(carBrandField_Sale, "cell 0 5,grow");

		modelField_Sale = new JTextField();
		makeSalePane.add(modelField_Sale, "cell 1 5,grow");
		modelField_Sale.setColumns(10);

		engineTypeField_Sale = new JTextField();
		makeSalePane.add(engineTypeField_Sale, "cell 2 5,grow");
		engineTypeField_Sale.setColumns(10);

		partNameField_Sale = new JTextField();
		makeSalePane.add(partNameField_Sale, "cell 3 5,grow");
		partNameField_Sale.setColumns(10);

		priceField_Sale = new JTextField();
		makeSalePane.add(priceField_Sale, "cell 4 5,grow");
		priceField_Sale.setColumns(10);

		numberField_Sale = new JTextField();
		makeSalePane.add(numberField_Sale, "cell 5 5,grow");
		numberField_Sale.setColumns(10);

		availableLabel_Sale = new JLabel("X Available");

		makeSalePane.add(availableLabel_Sale, "flowx,cell 6 5");

		btnAdd_Sale = new JButton("Add");
		btnAdd_Sale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				availableLabel_Sale.setText("X Available");
				btnAdd_Sale.setEnabled(false);
				carBrandField_Sale.setEnabled(true);
				carBrandField_Sale.setSelectedIndex(0);
				modelField_Sale.setEnabled(true);
				modelField_Sale.setText("");
				engineTypeField_Sale.setEnabled(true);
				engineTypeField_Sale.setText("");
				partNameField_Sale.setEnabled(true);
				partNameField_Sale.setText("");
				numberField_Sale.setEnabled(true);
				numberField_Sale.setText("");
				priceField_Sale.setEnabled(true);
				priceField_Sale.setText("");
				
				boolean alreadyIn = false;
				for (ProductLine productLine : sale.getSaleList())
				{
					if (productLine.getProduct().getProductId() == currentSaleProductLine.getProduct().getProductId())
					{
						productLine.setQuantity(productLine.getQuantity() + currentSaleProductLine.getQuantity());
						alreadyIn = true;
					}
				}
				if(!alreadyIn)
				{
					sale.insertProductLine(currentSaleProductLine);
				}
				insertInTable(makeSaleTable, sale.getSaleList());
				subtotalLabel_Sale.setText("" + sale.getTotal() + " DKK");
			}
		});

		JButton btnSearch_Sale = new JButton("Search");
		btnSearch_Sale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!numberField_Sale.getText().equals("")) {
					searchingForSaleProduct();
				} else {
					String quantity = JOptionPane
							.showInputDialog(
									null,
									(Object) "What is the quantity you are looking for?",
									"Quantity not specified.",
									JOptionPane.INFORMATION_MESSAGE);

					while (quantity.equals("")) {
						quantity = JOptionPane
								.showInputDialog(
										null,
										(Object) "What is the quantity you are looking for?",
										"Quantity not specified.",
										JOptionPane.INFORMATION_MESSAGE);
					}
					numberField_Sale.setText(quantity);
					searchingForSaleProduct();
				}
			}
		});
		makeSalePane.add(btnSearch_Sale, "cell 6 5");
		btnAdd_Sale.setEnabled(false);
		makeSalePane.add(btnAdd_Sale, "cell 6 5,grow");

		JSeparator separator_16 = new JSeparator();
		makeSalePane.add(separator_16, "cell 0 6 7 1,growx");

		JLabel lblReceipt = new JLabel("Receipt");
		lblReceipt.setFont(new Font("Tahoma", Font.BOLD, 24));
		makeSalePane.add(lblReceipt, "cell 0 7 7 1,alignx center");

		JSeparator separator_17 = new JSeparator();
		makeSalePane.add(separator_17, "cell 0 8 7 1,growx");

		JLabel lblSubtotal = new JLabel("Subtotal:");
		lblSubtotal.setFont(new Font("Tahoma", Font.BOLD, 19));
		makeSalePane.add(lblSubtotal, "cell 0 9,alignx right");

		subtotalLabel_Sale = new JLabel("0 DKK");
		subtotalLabel_Sale.setFont(new Font("Tahoma", Font.BOLD, 19));
		makeSalePane.add(subtotalLabel_Sale, "cell 1 9,alignx trailing");

		JLabel lblCustomerPhone = new JLabel("Customer phone / cpr / email:");
		makeSalePane.add(lblCustomerPhone, "cell 4 9,alignx trailing,growy");

		textField = new JTextField();
		makeSalePane.add(textField, "cell 5 9,grow");
		textField.setColumns(10);

		JButton btnFinishSale = new JButton("Finish Sale");
		btnFinishSale.addActionListener(new ActionListener() {
			private SaleCtr saleCtr = new SaleCtr();

			public void actionPerformed(ActionEvent arg0) {

				try {
					saleCtr.insertSale(sale);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sale = new Sale();
				availableLabel_Sale.setText("X Available");
				btnAdd_Sale.setEnabled(false);
				carBrandField_Sale.setEnabled(true);
				carBrandField_Sale.setSelectedIndex(0);
				modelField_Sale.setEnabled(true);
				modelField_Sale.setText("");
				engineTypeField_Sale.setEnabled(true);
				engineTypeField_Sale.setText("");
				partNameField_Sale.setEnabled(true);
				partNameField_Sale.setText("");
				numberField_Sale.setEnabled(true);
				numberField_Sale.setText("");
				priceField_Sale.setEnabled(true);
				priceField_Sale.setText("");

				insertInTable(makeSaleTable, sale.getSaleList());
				JOptionPane.showMessageDialog(null,
						(Object) "Everything went fine, sale was made!",
						"Input Success!", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		makeSalePane.add(btnFinishSale, "cell 6 9,growx");

		JScrollPane scrollPane_1 = new JScrollPane();
		makeSalePane.add(scrollPane_1, "cell 0 10 7 1,grow");

		makeSaleTable = new JTable();
		scrollPane_1.setViewportView(makeSaleTable);
		makeSaleTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		makeSaleTable.setCellSelectionEnabled(true);
		makeSaleTable.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null }, }, new String[] {
				"Car Brand", "Model", "Engine Type", "Name", "Price",
				"Quantity" }) {
			Class[] columnTypes = new Class[] { String.class, String.class,
					String.class, String.class, Double.class, Integer.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});

		JPanel makeOrderPane = new JPanel();
		tabbedPane.addTab("Make Order", null, makeOrderPane, null);
		makeOrderPane.setLayout(new MigLayout("",
				"[grow][grow][grow][grow][grow][grow][grow]",
				"[][][][][][][][][][][grow]"));

		JButton btnMainMenu_2 = new JButton("Main Menu");
		btnMainMenu_2.setFocusable(false);
		btnMainMenu_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.setSelectedIndex(0);// select and show the pane from
												// JTabbedPane with index 0
												// (Main Menu tab)
			}
		});
		btnMainMenu_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
		makeOrderPane.add(btnMainMenu_2, "cell 0 0 7 1,growx");

		JSeparator separator_23 = new JSeparator();
		makeOrderPane.add(separator_23, "cell 0 1 7 1,grow");

		JLabel lblOrder = new JLabel("Ordering Products");
		lblOrder.setFont(new Font("Tahoma", Font.BOLD, 24));
		makeOrderPane.add(lblOrder, "cell 0 2 7 1,alignx center");

		JSeparator separator_18 = new JSeparator();
		makeOrderPane.add(separator_18, "cell 0 3 7 1,growx");

		JLabel lblCarBrand_3 = new JLabel("Car Brand");
		makeOrderPane.add(lblCarBrand_3, "cell 0 4,alignx center");

		JLabel lblModel_5 = new JLabel("Model");
		makeOrderPane.add(lblModel_5, "cell 1 4,alignx center,growy");

		JLabel lblEngineType_5 = new JLabel("Engine type");
		makeOrderPane.add(lblEngineType_5, "cell 2 4,alignx center,growy");

		JLabel lblPartName_2 = new JLabel("Part name");
		makeOrderPane.add(lblPartName_2, "cell 3 4,alignx center,growy");

		JLabel lblPrice_5 = new JLabel("Price");
		makeOrderPane.add(lblPrice_5, "cell 4 4,alignx center,growy");

		JLabel lblNumber_4 = new JLabel("Quantity");
		makeOrderPane.add(lblNumber_4, "cell 5 4,alignx center,growy");

		JComboBox carBrandField_Order = new JComboBox();
		carBrandField_Order.setModel(new DefaultComboBoxModel(new String[] {
				"", "Alfa Romeo", "Audi", "BMW", "Chevrolet", "Chrysler",
				"Citro\u00EBn", "Dacia", "Daewoo", "Daihatsu", "Dodge", "Fiat",
				"Ford", "Honda", "Hyundai", "Iveco", "Jaguar", "Jeep", "Kia",
				"Lancia", "Land Rover", "Lexus", "Mazda", "Mercedes", "Mini",
				"Mitsubishi", "Nissan", "Opel", "Peugeot", "Porsche",
				"Renault", "Rover", "Saab", "Seat", "Skoda", "Smart", "Subaru",
				"Suzuki", "Toyota", "Volkswagen", "Volvo" }));
		makeOrderPane.add(carBrandField_Order, "cell 0 5,growx");

		modelField_Order = new JTextField();
		makeOrderPane.add(modelField_Order, "cell 1 5,grow");
		modelField_Order.setColumns(10);

		engineTypeField_Order = new JTextField();
		makeOrderPane.add(engineTypeField_Order, "cell 2 5,grow");
		engineTypeField_Order.setColumns(10);

		partNameField_Order = new JTextField();
		makeOrderPane.add(partNameField_Order, "cell 3 5,grow");
		partNameField_Order.setColumns(10);

		priceField_Order = new JTextField();
		makeOrderPane.add(priceField_Order, "cell 4 5,grow");
		priceField_Order.setColumns(10);

		numberField_Order = new JTextField();
		makeOrderPane.add(numberField_Order, "cell 5 5,grow");
		numberField_Order.setColumns(10);

		JSeparator separator_20 = new JSeparator();
		makeOrderPane.add(separator_20, "cell 0 6 7 1,grow");

		JLabel lblReceipt_1 = new JLabel("Receipt");
		lblReceipt_1.setFont(new Font("Tahoma", Font.BOLD, 24));
		makeOrderPane.add(lblReceipt_1, "cell 0 7 7 1,alignx center");

		JLabel label_4 = new JLabel("X Available");
		makeOrderPane.add(label_4, "flowx,cell 6 5");

		JButton btnOrder = new JButton("Add To Order");
		makeOrderPane.add(btnOrder, "cell 6 5,growx");

		JSeparator separator_19 = new JSeparator();
		makeOrderPane.add(separator_19, "cell 0 8 7 1,growx");

		JLabel label_5 = new JLabel("Subtotal:");
		label_5.setFont(new Font("Tahoma", Font.BOLD, 19));
		makeOrderPane.add(label_5, "cell 0 9,alignx right");

		JLabel label_6 = new JLabel("0 DKK");
		label_6.setFont(new Font("Tahoma", Font.BOLD, 19));
		makeOrderPane.add(label_6, "cell 1 9,alignx trailing");

		JLabel label_7 = new JLabel("Customer phone / cpr / email:");
		makeOrderPane.add(label_7, "cell 4 9,alignx trailing,growy");

		txtPhoneNumber = new JTextField();
		makeOrderPane.add(txtPhoneNumber, "cell 5 9,grow");
		txtPhoneNumber.setColumns(10);

		JButton btnFinishOrder = new JButton("Finish Order");
		makeOrderPane.add(btnFinishOrder, "cell 6 9,grow");

		JScrollPane scrollPane_2 = new JScrollPane();
		makeOrderPane.add(scrollPane_2, "cell 0 10 7 1,grow");

		table_2 = new JTable();
		scrollPane_2.setViewportView(table_2);
		table_2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table_2.setCellSelectionEnabled(true);
		table_2.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null }, }, new String[] {
				"Car Brand", "Model", "Engine Type", "Name", "Price",
				"Quantity" }) {
			Class[] columnTypes = new Class[] { String.class, String.class,
					String.class, String.class, Double.class, Integer.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});

		JPanel searchPane = new JPanel();
		tabbedPane.addTab("Search", null, searchPane, null);
		searchPane.setLayout(new MigLayout("",
				"[grow][grow][grow][grow][grow][grow][grow]",
				"[][][][][][][][grow]"));

		JButton btnNewButton = new JButton("Main Menu");
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnNewButton.setFocusable(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tabbedPane.setSelectedIndex(0);// select and show the pane from
												// JTabbedPane with index 0
												// (Main Menu tab)
			}
		});
		searchPane.add(btnNewButton, "cell 0 0 7 1,grow");

		JLabel lblSearchingProductsIn = new JLabel(
				"Searching Products in Stock");
		lblSearchingProductsIn.setFont(new Font("Tahoma", Font.BOLD, 24));
		searchPane.add(lblSearchingProductsIn, "cell 1 1 5 1,alignx center");

		JSeparator separator_14 = new JSeparator();
		searchPane.add(separator_14, "cell 0 2 7 1,growx");

		JLabel lblCarBrand_1 = new JLabel("Car Brand");
		searchPane.add(lblCarBrand_1, "cell 1 3,alignx center");

		JLabel lblModel_1 = new JLabel("Model");
		searchPane.add(lblModel_1, "cell 2 3,alignx center");

		JLabel lblEngineType_1 = new JLabel("Engine type");
		searchPane.add(lblEngineType_1, "cell 3 3,alignx center");

		JLabel lblName_1 = new JLabel("Name");
		searchPane.add(lblName_1, "cell 4 3,alignx center");

		final JComboBox<String> carBrand_Search = new JComboBox<String>();
		carBrand_Search.setModel(new DefaultComboBoxModel(new String[] { "",
				"Alfa Romeo", "Audi", "BMW", "Chevrolet", "Chrysler",
				"Citro\u00EBn", "Dacia", "Daewoo", "Daihatsu", "Dodge", "Fiat",
				"Ford", "Honda", "Hyundai", "Iveco", "Jaguar", "Jeep", "Kia",
				"Lancia", "Land Rover", "Lexus", "Mazda", "Mercedes", "Mini",
				"Mitsubishi", "Nissan", "Opel", "Peugeot", "Porsche",
				"Renault", "Rover", "Saab", "Seat", "Skoda", "Smart", "Subaru",
				"Suzuki", "Toyota", "Volkswagen", "Volvo" }));
		carBrand_Search.setName("carBrand");
		carBrand_Search.setEditable(true);
		searchPane.add(carBrand_Search, "cell 1 4,grow");

		carModel_Search = new JTextField();
		searchPane.add(carModel_Search, "cell 2 4,grow");
		carModel_Search.setColumns(15);

		carEngineType_Search = new JTextField();
		searchPane.add(carEngineType_Search, "cell 3 4,grow");
		carEngineType_Search.setColumns(15);

		carPartName_Search = new JTextField();
		searchPane.add(carPartName_Search, "cell 4 4,grow");
		carPartName_Search.setColumns(30);

		JButton searchButton_Search = new JButton("Search");
		searchButton_Search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product product = new Product();
				product.setCarBrand(((String) carBrand_Search.getSelectedItem())
						.toUpperCase());
				product.setEngineType((String) carEngineType_Search.getText()
						.toUpperCase());
				product.setCarModel((String) carModel_Search.getText()
						.toUpperCase());
				product.setPartName((String) carPartName_Search.getText()
						.toUpperCase());
				try {
					insertInTable(searchProductTable,
							productCtr.getAllProducts(product));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		searchPane.add(searchButton_Search, "cell 5 4,grow");

		JButton showAllButton_Search = new JButton("Show All");
		showAllButton_Search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					insertInTable(searchProductTable,
							productCtr.getAllProducts());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		searchPane.add(showAllButton_Search, "cell 6 4,grow");

		JSeparator separator_13 = new JSeparator();
		searchPane.add(separator_13, "cell 0 5 7 1,growx");

		JLabel label_3 = new JLabel("Car Brand");
		searchPane.add(label_3, "cell 1 6,alignx center");

		JLabel lblModel_2 = new JLabel("Model");
		searchPane.add(lblModel_2, "cell 2 6,alignx center");

		JLabel lblEngineType_2 = new JLabel("Engine type");
		searchPane.add(lblEngineType_2, "cell 3 6,alignx center");

		JLabel lblName_2 = new JLabel("Name");
		searchPane.add(lblName_2, "cell 4 6,alignx center");

		JLabel lblPrice_2 = new JLabel("Price");
		searchPane.add(lblPrice_2, "cell 5 6,alignx center");

		JLabel lblNumber_1 = new JLabel("Number");
		searchPane.add(lblNumber_1, "cell 6 6,alignx center");

		JScrollPane scrollPane_3 = new JScrollPane();
		searchPane.add(scrollPane_3, "cell 0 7 7 1,grow");

		searchProductTable = new JTable();
		scrollPane_3.setViewportView(searchProductTable);
		searchProductTable.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null }, }, new String[] {
				"Car Brand", "Model", "Engine Type", "Name", "Price",
				"Quantity" }) {
			Class[] columnTypes = new Class[] { String.class, String.class,
					String.class, String.class, Double.class, Integer.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			boolean[] columnEditables = new boolean[] { false, false, false,
					false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		searchProductTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int row = searchProductTable.getSelectedRow();
				if (row >= 0) {
					String carBrand = (String) searchProductTable.getModel()
							.getValueAt(row, 0);
					String carModel = (String) searchProductTable.getModel()
							.getValueAt(row, 1);
					String engineType = (String) searchProductTable.getModel()
							.getValueAt(row, 2);
					String partName = (String) searchProductTable.getModel()
							.getValueAt(row, 3);
					double price = -1;
					int stockQuantity = -1;
					if (searchProductTable.getModel().getValueAt(row, 4) != null) {
						price = (double) searchProductTable.getModel()
								.getValueAt(row, 4);
						stockQuantity = (int) searchProductTable.getModel()
								.getValueAt(row, 5);
					}
					Product product = new Product(-1, carBrand, carModel,
							engineType, partName, price, stockQuantity);
					product = productCtr.searchProduct(product);
					if (product.getProductId() >= 0) {
						EditProductGui editProductGui = new EditProductGui(
								product);
						editProductGui.setVisible(true);
					}
				}
			}
		});
		searchProductTable
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		JPanel reportsPane = new JPanel();
		tabbedPane.addTab("Reports", null, reportsPane, null);

		// execute table refresh
		try {
			insertInTable(addProductTable, productCtr.getAllProducts());
			insertInTable(searchProductTable, productCtr.getAllProducts());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public void searchingForSaleProduct() {
		String carBrand = (String) this.carBrandField_Sale.getSelectedItem();
		String carModel = modelField_Sale.getText();
		String engineType = engineTypeField_Sale.getText();
		String partName = partNameField_Sale.getText();
		int quantity = 1;
		if (!numberField_Sale.getText().equals("")) {
			quantity = Integer.parseInt(numberField_Sale.getText());
		}
		Product product = new Product();
		product.setCarBrand(carBrand);
		product.setCarModel(carModel);
		product.setEngineType(engineType);
		product.setPartName(partName);
		product.setStockQuantity(quantity);
		LinkedList<Product> productList = new LinkedList<Product>();
		try {
			productList = productCtr.getAllProducts(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (productList.size() == 1) {
			product = productList.getFirst();
			if (product.getProductId() >= 0) {
				availableLabel_Sale.setText("" + product.getStockQuantity()
						+ " Available");
				btnAdd_Sale.setEnabled(true);
				carBrandField_Sale.setEnabled(false);
				modelField_Sale.setEnabled(false);
				engineTypeField_Sale.setEnabled(false);
				partNameField_Sale.setEnabled(false);
				numberField_Sale.setEnabled(false);
				priceField_Sale.setEnabled(false);
				int remainingQtty = product.getStockQuantity();
				for (ProductLine productLine : sale.getSaleList())
				{
					if (productLine.getProduct().getProductId() == product.getProductId())
					{
						remainingQtty -= productLine.getQuantity();
					}
				}
				if (remainingQtty >= quantity) {
					currentSaleProductLine = new ProductLine(product, quantity);
					
				} else {
					JOptionPane.showMessageDialog(null,
							"Not enough products in the stock. There are currently "
									+ remainingQtty
									+ " products in stock.");
					availableLabel_Sale.setText("X Available");
					btnAdd_Sale.setEnabled(false);
					carBrandField_Sale.setEnabled(true);
					modelField_Sale.setEnabled(true);
					engineTypeField_Sale.setEnabled(true);
					partNameField_Sale.setEnabled(true);
					numberField_Sale.setEnabled(true);
					priceField_Sale.setEnabled(true);
				}
			}
		} else {
			JOptionPane
					.showMessageDialog(
							null,
							"Not enough input information was given, please fill more fields.",
							"Not Enough Information!", JOptionPane.YES_OPTION);
			availableLabel_Sale.setText("X Available");
			btnAdd_Sale.setEnabled(false);
			carBrandField_Sale.setEnabled(true);
			modelField_Sale.setEnabled(true);
			engineTypeField_Sale.setEnabled(true);
			partNameField_Sale.setEnabled(true);
			numberField_Sale.setEnabled(true);
			priceField_Sale.setEnabled(true);
		}
	}

	public static void refreshTables() {
		try {
			insertInTable(addProductTable, productCtr.getAllProducts());
			insertInTable(searchProductTable, productCtr.getAllProducts());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void emptyTable(JTable table) {
		TableModel tableModel = table.getModel();
		for (int i = 0; i <= tableModel.getRowCount() - 1; i++) {
			for (int j = 0; j <= tableModel.getColumnCount() - 1; j++) {
				tableModel.setValueAt(null, i, j);
			}
		}
	}

	private static void insertInTable(JTable table, LinkedList list) {
		emptyTable(table);
		TableModel tableModel = table.getModel();
		// int i = list.size() - 1;
		int i = 0;
		if (!list.isEmpty()) {
			if (list.get(0).getClass() == Product.class) {
				Iterator itr = list.iterator();
				while (itr.hasNext() && tableModel.getRowCount() > i) {
					Product product;
					product = (Product) itr.next();
					tableModel.setValueAt(product.getCarBrand(), i, 0);
					tableModel.setValueAt(product.getCarModel(), i, 1);
					tableModel.setValueAt(product.getEngineType(), i, 2);
					tableModel.setValueAt(product.getPartName(), i, 3);
					tableModel.setValueAt(product.getPrice(), i, 4);
					tableModel.setValueAt(product.getStockQuantity(), i, 5);
					i += 1;

				}
			} else if (list.get(0).getClass() == ProductLine.class) {
				Iterator itr = list.iterator();
				while (itr.hasNext() && tableModel.getRowCount() > i) {
					ProductLine productLine;
					productLine = (ProductLine) itr.next();
					tableModel.setValueAt(productLine.getProduct()
							.getCarBrand(), i, 0);
					tableModel.setValueAt(productLine.getProduct()
							.getCarModel(), i, 1);
					tableModel.setValueAt(productLine.getProduct()
							.getEngineType(), i, 2);
					tableModel.setValueAt(productLine.getProduct()
							.getPartName(), i, 3);
					tableModel.setValueAt(productLine.getProduct().getPrice(),
							i, 4);
					tableModel.setValueAt(productLine.getQuantity(), i, 5);
					i += 1;

				}
			}
		} else {
			tableModel.setValueAt(null, 0, 0);
			tableModel.setValueAt(null, 0, 1);
			tableModel.setValueAt(null, 0, 2);
			tableModel.setValueAt(null, 0, 3);
			tableModel.setValueAt(null, 0, 4);
			tableModel.setValueAt(null, 0, 5);
		}
		table.setModel(tableModel);

	}

}
